//
//  HeadView.m
//  Project_B
//
//  Created by 王涛 on 15/12/16.
//  Copyright © 2015年 王涛. All rights reserved.
//

#import "HeadView.h"
#import "Weather.h"
@implementation HeadView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        
        self.weatherImageView = [[UIImageView alloc] initWithFrame:(CGRectMake(kWidth / 37.5 , kHeight / 66.7 + 5, kWidth / 5.36 - 5, kHeight / 10.26 - 10))];
        self.weatherImageView.image = [UIImage imageNamed:@"d1@2x.png"];
        [self addSubview:self.weatherImageView];
        
        self.temperatureLabel = [[UILabel alloc] initWithFrame:(CGRectMake(kWidth / 4.6875 + 10, kHeight / 23.82, kWidth / 3.75, kHeight / 25))];
        self.temperatureLabel.font = [UIFont boldSystemFontOfSize:23];
        self.temperatureLabel.tintColor = [UIColor darkGrayColor];
        self.temperatureLabel.text = @"5°C";
        [self addSubview:self.temperatureLabel];
        
        UIImageView *imageView = [[UIImageView alloc]initWithFrame:(CGRectMake(kWidth / 1.39, kHeight / 33.35, kWidth / 4.41, kHeight / 9.53))];
        imageView.image = [UIImage imageNamed:@"air.png"];
        imageView.layer.borderWidth = 1;
        imageView.layer.cornerRadius = 5;
        imageView.layer.borderColor = [[UIColor lightGrayColor] CGColor];
        [self addSubview:imageView];
        
        self.airLabel = [[UILabel alloc] initWithFrame:(CGRectMake(kWidth / 1.39, kHeight / 14.82, kWidth / 4.41, kHeight / 16.675))];
        self.airLabel.textColor = [UIColor redColor];
        self.airLabel.textAlignment = NSTextAlignmentCenter;
        self.airLabel.font = [UIFont boldSystemFontOfSize:25];
        self.airLabel.textColor = [UIColor redColor];
        self.airLabel.text = @"150";
        [self addSubview:self.airLabel];
        
        self.xicheLabel = [[UILabel alloc] initWithFrame:(CGRectMake(kWidth / 18.75, kHeight / 8.89, kWidth / 2, kHeight / 33.35))];
        self.xicheLabel.font = [UIFont systemFontOfSize:14];
        self.xicheLabel.textColor = [UIColor lightGrayColor];
        self.xicheLabel.text = @"天气更新失败|请打开定位";
        [self addSubview:self.xicheLabel];
        
        self.addressLabel = [[UILabel alloc] initWithFrame:(CGRectMake(kWidth / 18.75, kHeight / 6.06, kWidth / 2, kHeight / 26.68))];
        self.addressLabel.textColor = [UIColor lightGrayColor];
        self.addressLabel.font = [UIFont systemFontOfSize:15];
        self.addressLabel.text = @"定位中...";
        [self addSubview:self.addressLabel];
        
        self.dateLabel = [[UILabel alloc] initWithFrame:(CGRectMake(kWidth / 1.42, kHeight / 6.06, kWidth / 2, kHeight / 26.68))];
        self.dateLabel.font = [UIFont systemFontOfSize:15];
        self.dateLabel.textColor = [UIColor lightGrayColor];
        [self addSubview:self.dateLabel];
        [self setDate];//直接调用系统时间
    }
    return self;
}
- (void)setWeather:(Weather *)weather {
    if (_weather != weather) {
        _weather = weather;
        self.temperatureLabel.text = weather.temperature;
        self.xicheLabel.text = [[[weather.wash_index stringByAppendingString:@"洗车"] stringByAppendingString:@"|"] stringByAppendingString:weather.weather];
        //        self.dateLabel.text = [[[[weather.date substringFromIndex:5] stringByReplacingOccurrencesOfString:@"-" withString:@"/"] stringByAppendingString:@"   "] stringByAppendingString:weather.week];
        self.addressLabel.text = [[weather.city stringByAppendingString:@" "] stringByAppendingString:weather.distrct];
        if ([weather.weather rangeOfString:@"云"].length > 0) {
            self.weatherImageView.image = [UIImage imageNamed:@"d1@2x.png"];
        }
        if ([weather.weather isEqualToString:@"阴"]) {
            self.weatherImageView.image = [UIImage imageNamed:@"d2@2x.png"];
        }
        if ([weather.weather isEqualToString:@"晴"]) {
            self.weatherImageView.image = [UIImage imageNamed:@"d0@2x.png"];
        }
        if ([weather.weather rangeOfString:@"雨"].length > 0) {
            self.weatherImageView.image = [UIImage imageNamed:@"d7@2x.png"];
        }
        if ([weather.weather rangeOfString:@"雪"].length > 0) {
            self.weatherImageView.image = [UIImage imageNamed:@"d26@2x.png"];
        }
        if ([weather.weather rangeOfString:@"风"].length > 0) {
            self.weatherImageView.image = [UIImage imageNamed:@"d9@2x.png"];
        }
        if ([weather.weather rangeOfString:@"雾"].length > 0) {
            self.weatherImageView.image = [UIImage imageNamed:@"d18@2x.png"];
        }
        if ([weather.weather rangeOfString:@"霜"].length > 0) {
            self.weatherImageView.image = [UIImage imageNamed:@"d29@2x.png"];
        }
    }
}

- (void)setDate {
    NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
    [formatter setDateFormat:@"yyyy-MM-dd"];
    NSString *string = [formatter stringFromDate:[NSDate date]];
    NSDate *date = [NSDate date];
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSDateComponents *comps;
    comps = [calendar components:(NSWeekCalendarUnit | NSWeekdayCalendarUnit | NSWeekdayOrdinalCalendarUnit)
                        fromDate:date];
    NSInteger weekday = [comps weekday]; // 星期几（注意，周日是“1”，周一是“2”。。。。）
    switch (weekday) {
        case 1:
            self.dateLabel.text = [[[string substringFromIndex:5] stringByReplacingOccurrencesOfString:@"-" withString:@"/"] stringByAppendingString:@" 星期日"];
            break;
        case 2:
            self.dateLabel.text = [[[string substringFromIndex:5] stringByReplacingOccurrencesOfString:@"-" withString:@"/"] stringByAppendingString:@" 星期一"];
            break;
        case 3:
            self.dateLabel.text = [[[string substringFromIndex:5] stringByReplacingOccurrencesOfString:@"-" withString:@"/"] stringByAppendingString:@" 星期二"];
            break;
        case 4:
            self.dateLabel.text = [[[string substringFromIndex:5] stringByReplacingOccurrencesOfString:@"-" withString:@"/"] stringByAppendingString:@" 星期三"];
            break;
        case 5:
            self.dateLabel.text = [[[string substringFromIndex:5] stringByReplacingOccurrencesOfString:@"-" withString:@"/"] stringByAppendingString:@" 星期四"];
            break;
        case 6:
            self.dateLabel.text = [[[string substringFromIndex:5] stringByReplacingOccurrencesOfString:@"-" withString:@"/"] stringByAppendingString:@" 星期五"];
            break;
        case 7:
            self.dateLabel.text = [[[string substringFromIndex:5] stringByReplacingOccurrencesOfString:@"-" withString:@"/"] stringByAppendingString:@" 星期六"];
            break;
            
        default:
            break;
    }
    
}



@end
